package com.gzhryc.shared_device.oem.h5.agent.mng.controllers.bxscn;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.bxscn.code.dao.db.PortableEnergyCabinet;
import com.gzhryc.shared_device.bxscn.code.dao.enums.PortableEnergyCabinetEnum;
import com.gzhryc.shared_device.bxscn.code.services.PortableEnergyCabinetService;
import com.gzhryc.shared_device.bxscn.core.IBXSCNNetworkService;
import com.gzhryc.shared_device.bxscn.core.models.BXSCNExecuteResult;
import com.gzhryc.shared_device.bxscn.core.models.BXSCNLeaseCommand;
import com.gzhryc.shared_device.bxscn.core.models.BXSCNOpenSlotCommand;
import com.gzhryc.shared_device.common.device.dao.db.Device;
import com.gzhryc.shared_device.common.device.services.DeviceService;
import com.gzhryc.shared_device.oem.code.HttpNetworkServiceUtils;
import com.gzhryc.shared_device.oem.code.device.BXSCNCacheManager;
import com.gzhryc.shared_device.oem.code.user.dao.db.User;
import com.gzhryc.shared_device.oem.h5.agent.mng.WebConstants;

import javax.servlet.http.HttpServletRequest;

public class PortableEnergyCabinetOperateController {

    @Route(value = {"/bxscn/device/operate/search.do"})
    public AjaxResult doSearch(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "便携式储能电池查询失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        if (StringTools.isNotBlank(deviceSn)) {
            User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
            if (loginUser != null) {
                Device device = DeviceService.self().getByDeviceSn(deviceSn);
                if (device != null && device.getUserId() != null && device.getUserId().equals(loginUser.getId())) {
                    PortableEnergyCabinet portableEnergyCabinet = PortableEnergyCabinetService.self().getByDeviceSn(deviceSn);
                    if (portableEnergyCabinet != null) {
                        if (PortableEnergyCabinetEnum.State.Online.index().equals(portableEnergyCabinet.getState())) {
                            //如果缓存不存在，代表没有上线
                            PortableEnergyCabinet temp = BXSCNCacheManager.self().getPortableEnergyCabinet(portableEnergyCabinet.getDeviceSn());
                            if (temp == null) {
                                PortableEnergyCabinetService.self().offline(portableEnergyCabinet.getDeviceSn());
                            }
                        }

                        IBXSCNNetworkService networkService = HttpNetworkServiceUtils.getBXSCNNetworkService(portableEnergyCabinet.getIotNodeId());
                        if (networkService != null) {
                            BXSCNExecuteResult executeResult = networkService.searchDevice(deviceSn);
                            if (executeResult != null) {
                                if (BXSCNExecuteResult.SUCCESS.equals(executeResult.getCode())) {
                                    result.setCode(DtoConstants.SUCCESS, null);
                                } else if (StringTools.isNotBlank(executeResult.getMsg())) {
                                    result.setMsg(executeResult.getMsg());
                                } else {
                                    result.setMsg("发送命令失败");
                                }
                            } else {
                                result.setMsg("便携式储能电池机柜网络服务无响应");
                            }
                        } else {
                            result.setMsg("未获取便携式储能电池机柜网络服务");
                        }
                    }
                }else {
                    result.setMsg("无权限操作");
                }
            }
        }
        return result;
    }

    @Route("/bxscn/device/operate/lease.html")
    public String lease(HttpServletRequest request) {
        return "/bxscn/device/lease.html";
    }

    @Route(value = {"/bxscn/device/operate/lease.do"})
    public AjaxResult doLease(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "自动租借失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        String tradeNo = WebUtils.getString(request, "tradeNo");
        Integer slotNum = WebUtils.getInteger(request, "slotNum");
        if (StringTools.isNotBlank(deviceSn) && StringTools.isNotBlank(tradeNo)) {
            User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
            if (loginUser != null) {
                Device device = DeviceService.self().getByDeviceSn(deviceSn);
                if (device != null && device.getUserId() != null && device.getUserId().equals(loginUser.getId())) {
                    PortableEnergyCabinet portableEnergyCabinet = PortableEnergyCabinetService.self().getByDeviceSn(deviceSn);
                    if (portableEnergyCabinet != null) {
                        IBXSCNNetworkService networkService = HttpNetworkServiceUtils.getBXSCNNetworkService(portableEnergyCabinet.getIotNodeId());
                        if (networkService != null) {
                            BXSCNLeaseCommand command = new BXSCNLeaseCommand();
                            command.setDeviceSn(deviceSn);
                            command.setTradeNo(tradeNo);
                            command.setSlotNum(slotNum);

                            BXSCNExecuteResult executeResult = networkService.lease(command);
                            if (executeResult != null) {
                                if (BXSCNExecuteResult.SUCCESS.equals(executeResult.getCode())) {
                                    result.setCode(DtoConstants.SUCCESS, null);
                                } else if (StringTools.isNotBlank(executeResult.getMsg())) {
                                    result.setMsg(executeResult.getMsg());
                                } else {
                                    result.setMsg("发送命令失败");
                                }
                            } else {
                                result.setMsg("便携式储能电池机柜网络服务无响应");
                            }
                        } else {
                            result.setMsg("未获取便携式储能电池机柜网络服务");
                        }
                    }
                }else {
                    result.setMsg("无权限操作");
                }
            }
        }
        return result;
    }

    @Route(value = {"/bxscn/device/operate/open_slot.do"})
    public AjaxResult doOpenSlot(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "自动租借失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        Integer slotNum = WebUtils.getInteger(request, "slotNum");
        if (StringTools.isNotBlank(deviceSn)) {
            User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
            if (loginUser != null) {
                Device device = DeviceService.self().getByDeviceSn(deviceSn);
                if (device != null && device.getUserId() != null && device.getUserId().equals(loginUser.getId())) {
                    PortableEnergyCabinet portableEnergyCabinet = PortableEnergyCabinetService.self().getByDeviceSn(deviceSn);
                    if (portableEnergyCabinet != null) {
                        IBXSCNNetworkService networkService = HttpNetworkServiceUtils.getBXSCNNetworkService(portableEnergyCabinet.getIotNodeId());
                        if (networkService != null) {
                            BXSCNOpenSlotCommand command = new BXSCNOpenSlotCommand();
                            command.setDeviceSn(deviceSn);
                            command.setSlotNum(slotNum);

                            BXSCNExecuteResult executeResult = networkService.openSlot(command);
                            if (executeResult != null) {
                                if (BXSCNExecuteResult.SUCCESS.equals(executeResult.getCode())) {
                                    result.setCode(DtoConstants.SUCCESS, null);
                                } else if (StringTools.isNotBlank(executeResult.getMsg())) {
                                    result.setMsg(executeResult.getMsg());
                                } else {
                                    result.setMsg("发送命令失败");
                                }
                            } else {
                                result.setMsg("便携式储能电池机柜网络服务无响应");
                            }
                        } else {
                            result.setMsg("未获取便携式储能电池机柜网络服务");
                        }
                    }
                }else {
                    result.setMsg("无权限操作");
                }
            }
        }
        return result;
    }

    @Route(value = {"/bxscn/device/operate/eject_all.do"})
    public AjaxResult doEjectAll(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "打开全部仓门失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        if (StringTools.isNotBlank(deviceSn)) {
            User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
            if (loginUser != null) {
                Device device = DeviceService.self().getByDeviceSn(deviceSn);
                if (device != null && device.getUserId() != null && device.getUserId().equals(loginUser.getId())) {
                    PortableEnergyCabinet portableEnergyCabinet = PortableEnergyCabinetService.self().getByDeviceSn(deviceSn);
                    if (portableEnergyCabinet != null) {
                        IBXSCNNetworkService networkService = HttpNetworkServiceUtils.getBXSCNNetworkService(portableEnergyCabinet.getIotNodeId());
                        if (networkService != null) {
                            BXSCNExecuteResult executeResult = networkService.forceEjectDevice(portableEnergyCabinet.getDeviceSn());
                            if (executeResult != null) {
                                if (BXSCNExecuteResult.SUCCESS.equals(executeResult.getCode())) {
                                    result.setCode(DtoConstants.SUCCESS, null);
                                } else if (StringTools.isNotBlank(executeResult.getMsg())) {
                                    result.setMsg(executeResult.getMsg());
                                } else {
                                    result.setMsg("发送命令失败");
                                }
                            } else {
                                result.setMsg("便携式储能电池机柜网络服务无响应");
                            }
                        } else {
                            result.setMsg("未获取便携式储能电池机柜网络服务");
                        }
                    }
                }else {
                    result.setMsg("无权限操作");
                }
            }
        }
        return result;
    }

    @Route(value = {"/bxscn/device/operate/eject.do"})
    public AjaxResult doEject(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "开仓失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        Integer slotNum = WebUtils.getInteger(request, "slotNum");
        if (StringTools.isNotBlank(deviceSn) && slotNum != null) {
            User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
            if (loginUser != null) {
                Device device = DeviceService.self().getByDeviceSn(deviceSn);
                if (device != null && device.getUserId() != null && device.getUserId().equals(loginUser.getId())) {
                    PortableEnergyCabinet portableEnergyCabinet = PortableEnergyCabinetService.self().getByDeviceSn(deviceSn);
                    if (portableEnergyCabinet != null) {
                        IBXSCNNetworkService networkService = HttpNetworkServiceUtils.getBXSCNNetworkService(portableEnergyCabinet.getIotNodeId());
                        if (networkService != null) {
                            BXSCNExecuteResult executeResult = networkService.forceEjectDevice(deviceSn, slotNum);
                            if (executeResult != null) {
                                if (BXSCNExecuteResult.SUCCESS.equals(executeResult.getCode())) {
                                    result.setCode(DtoConstants.SUCCESS, null);
                                } else if (StringTools.isNotBlank(executeResult.getMsg())) {
                                    result.setMsg(executeResult.getMsg());
                                } else {
                                    result.setMsg("发送命令失败");
                                }
                            } else {
                                result.setMsg("便携式储能电池机柜网络服务无响应");
                            }
                        } else {
                            result.setMsg("未获取便携式储能电池机柜网络服务");
                        }
                    }
                }else {
                    result.setMsg("无权限操作");
                }
            }
        }
        return result;
    }

}
